package demo4;
/*

class Student implements Comparable<Student> {
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
    }
}

public class Test {
    public static void main(String[] args) {
        Student student1 = new Student("zhangsan", 14);
        Student student2 = new Student("lisi", 12);
        System.out.println(student1.compareTo(student2));
        System.out.println();
    }
}
*/


import java.util.Arrays;
import java.util.Comparator;

class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        //System.out.println("-------------");
        return this.age - o.age;
    }
}

public class Test {
    public static void main(String[] args) {

        Student[] students = new Student[3];
        students[0] = new Student("zhangsan", 14);
        students[1] = new Student("lisi", 12);
        students[2] = new Student("abc", 2);
        System.out.println("排序前：" + Arrays.toString(students));
        Arrays.sort(students);
        System.out.println("排序后：" + Arrays.toString(students));
    }


    /*public static void main1(String[] args) {
        Student student1 = new Student("zhangsan", 14);
        Student student2 = new Student("lisi", 12);

        AgeComparator ageComparator = new AgeComparator();
        System.out.println(ageComparator.compare(student1, student2));

        NameComparator nameComparator = new NameComparator();
        System.out.println(nameComparator.compare(student1, student2));

    }*/
}

